﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Microsoft.Phone.Data.Linq;
using Microsoft.Phone.Data.Linq.Mapping;
using System.ComponentModel;
using System.IO;
using System.Threading;

namespace WeiboBulletin.Model
{
    public class Setting
    {
        private static readonly string FileName = "Setting.txt";
        private static readonly Mutex FileMutex = new Mutex(false, FileName);
        // Weibo Account
        public string UserID;
        public string AccessToken;
        public string AccessSecret;

        public int UpdateIntervalInMinutesInWifi = 60;  // update for every 1 min.
        public int UpdateIntervalInMintuesOffWifi = 5 * 60; // Update for every 5 mins.

        public string LastPostCreateTime;
        public string LastShowedPostCreateTime;

        // PushServiceChannel
        public string PushServiceChannelUrl;

        public static Setting GetCurrentAppSetting()
        {
            Setting ret = new Setting();

            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            FileMutex.WaitOne();
            try
            {
                using (var ins = new IsolatedStorageFileStream(Setting.FileName, FileMode.Open, store))
                {
                    using (var reader = new StreamReader(ins))
                    {
                        ret.UserID = reader.ReadLine();
                        ret.AccessToken = reader.ReadLine();
                        ret.AccessSecret = reader.ReadLine();
                        ret.UpdateIntervalInMinutesInWifi = int.Parse(reader.ReadLine());
                        ret.LastPostCreateTime = reader.ReadLine();
                        ret.LastShowedPostCreateTime = reader.ReadLine();
                        //ret.PushServiceChannelUrl = reader.ReadLine();
                    }
                }
            }
            catch
            {
            }
            finally
            {
                FileMutex.ReleaseMutex();
            }

            return ret;
        }

        public static void SetCurrentAppSetting(Setting setting)
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            FileMutex.WaitOne();
            try
            {
                using (var os = new IsolatedStorageFileStream(Setting.FileName, FileMode.Create, store))
                {
                    using (var writer = new StreamWriter(os))
                    {
                        writer.WriteLine(setting.UserID);
                        writer.WriteLine(setting.AccessToken);
                        writer.WriteLine(setting.AccessSecret);
                        writer.WriteLine(setting.UpdateIntervalInMinutesInWifi.ToString());
                        writer.WriteLine(setting.LastPostCreateTime);
                        writer.WriteLine(setting.LastShowedPostCreateTime);
                        //writer.WriteLine(setting.PushServiceChannelUrl);
                    }
                }
            }
            finally
            {
                FileMutex.ReleaseMutex();
            }
        }
    }
}
